<?php

/**
 * Class WenHuiLogis
 */
namespace App\Utility\Logis\Wenhui;

use App\Utility\Logis\LogisApiAbstract;

class WenHuiLogis extends LogisApiAbstract
{
    protected static $_self = null;
    protected $appToken = '';
    protected $appKey = '';
    protected $soapMethod = '';

    protected $sendType = 'webservice';

    public static $sandBox = false;

    /**
     * 设置参数
     */
    protected function __construct()
    {
        if (self::$sandBox) {
            $this->apiUrl = 'http://120.76.152.245/default/svc/wsdl';
            $this->appToken = 'a90a74d01224a7709351dbd77b665f47';
            $this->appKey = 'a90a74d01224a7709351dbd77b665f47ccf5beb50a0fcc47f34c9554d0f5f8e5';
        } else {
            $this->apiUrl = 'http://120.76.152.245/default/svc/wsdl';
            $this->appToken = 'b826956f5bc96a4d1abc5bf2ee3bdf9b';
            $this->appKey = 'b826956f5bc96a4d1abc5bf2ee3bdf9bb42db62a62543292516060b4a791a445';
        }
    }

    /**
     * 单例
     * @return mixed
     */
    public static function getInstance()
    {
        if (is_null(self::$_self)) {
            self::$_self = new self();
        }
        return self::$_self;
    }

    /**
     * 设置soap的请求方法(特殊处理)
     * @param $soapMethod
     * @return $this
     */
    public function setSoapMethod($soapMethod)
    {
        $this->soapMethod = $soapMethod;
        return $this;
    }

    /**
     * 生成请求参数
     */
    public function createRequestByType()
    {
        $json = \json_encode($this->requestBody);
        $this->requestBody = array(
            array(
                'paramsJson' => $json,
                'appToken' => $this->appToken,
                'appKey' => $this->appKey,
                'service' => $this->soapMethod
            )
        );
    }

    /**
     * 解析结果
     */
    public function parseResponseByType($response)
    {
        $return = array(
            'status' => false,
            'errorMsg' => '返回结果为空'
        );

        if ($response) {
            try {
                if ($response && isset($response->response)) {
                    $response = json_decode($response->response);
                    if (json_last_error() > 0) {
                        throw new \Exception('response解析json错误:' . serialize($response->response));
                    }
                    if ($response->ask == 'Success') {
                        $return = array('status' => true);
                        if (isset($response->data)) {
                            $return['data'] = $response->data;
                        } else {
                            $return['data'] = $response;
                        }
                    } else {
                        if (isset($response->Error) && !empty($response->Error->errMessage)) {
                            $errorCode = isset($response->Error->errCode) ? $response->Error->errCode : '';
                            $return = array(
                                'status' => false,
                                'errorMsg' => '接口返回错误：errorCode:' . $errorCode . ';errorMsg:' . $response->Error->errMessage
                            );
                        } else {
                            $errorCode = !empty($response->err_code) ? $response->err_code : '';
                            $errorMsg = !empty($response->errMessage) ? $response->errMessage : '未知错误';
                            $return = array(
                                'status' => false,
                                'errorMsg' => '接口返回错误：errorCode:' . $errorCode . ';errorMsg:' . $errorMsg
                            );
                        }
                    }
                } else {
                    $return = array(
                        'status' => false,
                        'errorMsg' => '接口返回的结果集异常:result->' . serialize($response)
                    );
                }
            } catch (\Exception $e) {
                $return['errorMsg'] = '解析结果Exception:' . $e->getMessage();
            }
        }
        return $return;
    }
}